package com.example.huichaoliu;

import org.json.JSONException;
import org.json.JSONObject;

import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.huichaoliu.app.R;
import com.huichaoliu.data.Config;
import com.huichaoliu.data.UserInfo;
import com.rederxu.farmer.BaseActivity;
import com.rederxu.request.HTTPRequest;
import com.rederxu.request.onRequestOverListener;
import com.rederxu.tools.StringUtils;
import com.source.utils.Utils;

public class FindPasswordActivity extends BaseActivity implements OnClickListener {

	private EditText et_mobile = null;
	private EditText et_password = null;
	private EditText et_code = null;
	private Button btn_get_code = null;
	private String mobile_str = null;
	private int random = 0;
	private int lastSecond = -1;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_find_pwd);
		super.onCreate(savedInstanceState);
	}

	@Override
	public void onClick(View v) {
		switch(v.getId()){
		case R.id.left_button:
			finish();
			break;
		case R.id.btn_regist:
			String pwd_str = et_password.getText().toString();
			String code_str = et_code.getText().toString();
			if(StringUtils.isBlank(mobile_str) || mobile_str.length() != 11){
				showToast("请输入合法手机号");
				return;
			}
			if(pwd_str.length() < 6 || pwd_str.length() > 16){
				showToast("请输入6~16位密码");
				return;
			}
			if(code_str.length() != 6 || Integer.parseInt(code_str) != random){
				showToast("请输入正确验证码");
				return;
			}
			try{
				JSONObject jo = new JSONObject();//sendregisterSMS.action
				jo.put("phone", mobile_str);
				jo.put("pass", Utils.stringToMD5(pwd_str));
				Bundle bundle = new Bundle();
				bundle.putString("request", Utils.requestParamsEncode(jo));
				HTTPRequest.POST(this, Config.REQUEST_URLS.URL_USER_PASSALTER, bundle, new onRequestOverListener() {
					@Override
					public void onRequestOver(String result) {
						try{
							JSONObject jo = new JSONObject(result);
							showToast(jo.getString("message"));
							if(jo.getInt("code") != 0){
								return;
							}
							jo = jo.getJSONObject("response");
							UserInfo info = new UserInfo(FindPasswordActivity.this);
							info.setMobile(jo.getString("phone"));
							info.setNickname(jo.getString("nickname"));
							info.setUid(jo.getString("userId"));
							info.commit();
							finish();
						}catch(JSONException e){
							e.printStackTrace();
						}
					}
				});
			}catch(Exception e){
				
			}
			break;
		case R.id.btn_get_code:
			mobile_str = et_mobile.getText().toString();
			if(StringUtils.isBlank(mobile_str) || mobile_str.length() != 11){
				showToast("请输入合法手机号");
				return;
			}
			try{
				JSONObject jo = new JSONObject();
				jo.put("phone", mobile_str);
				Bundle bundle = new Bundle();
				bundle.putString("request", Utils.requestParamsEncode(jo));
				HTTPRequest.POST(this, Config.REQUEST_URLS.URL_SEND_FIND_PASSWORD_SMS, bundle, new onRequestOverListener() {
					
					@Override
					public void onRequestOver(String result) {
						Log.e("result", result);
						try{
							JSONObject jo = new JSONObject(result);
							if(jo.getInt("code") != 0){
								showToast(jo.getString("message"));
								return;
							}
							showToast(jo.getString("message"));
							jo = jo.getJSONObject("response");
							random = jo.getInt("random");
							lastSecond = 120;
							btn_get_code.setTextColor(Color.GRAY);
							btn_get_code.setClickable(false);
							btn_get_code.setBackgroundResource(R.drawable.bg_btn_unclickable);
						}catch(JSONException e){
							e.printStackTrace();
						}
					}
				});
			}catch(Exception e){
				
			}
			break;
		}
	}

	@Override
	public void init() {
		((TextView) findViewById(R.id.title_text)).setText("找回密码");
		btn_get_code = (Button) findViewById(R.id.btn_get_code);
		findViewById(R.id.left_button).setOnClickListener(this);
//		findViewById(R.id.btn_regist).setOnClickListener(this);
		btn_get_code.setOnClickListener(this);
		
		et_mobile = (EditText) findViewById(R.id.et_mobile);
		et_password = (EditText) findViewById(R.id.et_password);
		et_code = (EditText) findViewById(R.id.et_code);
		
		UserInfo info = new UserInfo(this);
		et_mobile.setText(info.getMobile());
		
		new Thread(){
			@Override
			public void run() {
				while(true){
					lastSecond --;
					if(lastSecond > -1){
						handler.sendEmptyMessage(0);
					}else{
						lastSecond = -1;
					}
					try {
						Thread.sleep(1000);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}
		}.start();
	}

	@Override
	public void initData() {
		// TODO Auto-generated method stub

	}

	@Override
	public void onNetworkChanged(boolean isMobNetConnected,
			boolean isWifiConnected) {
		// TODO Auto-generated method stub

	}

	private Handler handler = new Handler(){
		@Override
		public void handleMessage(Message msg) {
			if(lastSecond == 0){
				btn_get_code.setTextColor(Color.WHITE);
				btn_get_code.setClickable(true);
				btn_get_code.setBackgroundResource(R.drawable.bg_btn);
				btn_get_code.setText("点击重新获取验证码");
			}else{
				btn_get_code.setText(lastSecond + "s后可重新获取");
			}
			super.handleMessage(msg);
		}
	};
}
